package com.hnit.rol.order.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hnit.rol.commonutils.RandomUtil;
import com.hnit.rol.commonutils.Result;
import com.hnit.rol.model.entity.bookInfo.BookInfo;
import com.hnit.rol.model.entity.order.OrderInfo;
import com.hnit.rol.model.entity.order.OrderInfoShow;
import com.hnit.rol.model.vo.book.user.MemberVO;
import com.hnit.rol.model.vo.bookShow.BookShowVo;
import com.hnit.rol.model.vo.order.OrderInfoVo;
import com.hnit.rol.model.vo.order.OrderQueryCondition;
import com.hnit.rol.order.mapper.OrderInfoMapper;
import com.hnit.rol.order.service.IOrderInfoService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hnit.rol.servicebase.feignClient.BookClient;
import com.hnit.rol.servicebase.feignClient.UcenterClient;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.UUID;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author King
 * @since 2023-04-08
 */
@Service
public class OrderInfoServiceImpl extends ServiceImpl<OrderInfoMapper, OrderInfo> implements IOrderInfoService {


    /**
     * 分页查询订单信息
     *
     * @param index          当前页
     * @param limit          每页记录数
     * @param queryCondition 查询条件
     * @return 分页查询获取的订单列表
     */
    @Override
    public Page<OrderInfo> pageQueryOrder(Long index, Long limit, OrderQueryCondition queryCondition) {
        Page<OrderInfo> orderPage = new Page<>(index, limit);
        // 获取查询条件
        String beginTime = queryCondition.getBeginTime();
        String endTime = queryCondition.getEndTime();
        // 设定查询条件
        LambdaQueryWrapper<OrderInfo> wrapper = new LambdaQueryWrapper<>();
        wrapper.ge(StringUtils.hasText(beginTime), OrderInfo::getCreateTime, beginTime);
        wrapper.le(StringUtils.hasText(endTime), OrderInfo::getCreateTime, endTime);
        // 获取数据
        return baseMapper.selectPage(orderPage, wrapper);
    }

    //生成订单信息，需要书籍信息和用户信息
    @Override
    public OrderInfoShow generateOrder(Long id) {

        OrderInfoShow bookInfo = baseMapper.getUserOrderInfoById(id);

        return bookInfo;
    }

    @Override
    public void insertShoppingCart(OrderInfo order) {
        //生成一个随机id作为订单id
        String orderId;
        orderId = RandomUtil.getSixBitRandom();
        order.setOrderId(Long.valueOf(orderId));
        baseMapper.insert(order);
//        if(insert>0){
//            return Result.success();
//        }else{
//            return Result.failure().message("存入数据失败");
//        }
    }
}
